package com.soft.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.soft.entity.TCart;
import com.soft.entity.TProduct;
import com.soft.entity.TUser;
import com.soft.service.TCartService;
import com.soft.service.TProductService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * (TProduct)表控制层
 *
 * @author makejava
 * @since 2021-12-23 19:18:11
 */
@Controller
@RequestMapping("/tProduct")
public class TProductController {
    private TUser login;
    /**
     * 服务对象
     */
    @Resource
    private TProductService tProductService;
    @Resource
    private TCartService tCartService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public TProduct selectOne(Integer id) {
        return this.tProductService.queryById(id);
    }

    @RequestMapping("products")
//    @ResponseBody
    public String getAllproducts(Integer offset, Integer limit, Model model , HttpSession session){

        PageInfo<TProduct> pageInfo = tProductService.queryAllByPageHelper(offset, limit);
        System.out.println(pageInfo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("msg", "Controller中的传过来的值");


        login = (TUser) session.getAttribute("user");
        List<TCart> list = tCartService.findAllCartAssociateProductByUserId(login.getId());//session.getAttribute("user").getId()
        TCartController.cartBar(list, model);
//        return pageInfo;
        return "/user/shop";
    }
    @RequestMapping("admin/products")
//    @ResponseBody
    public String getAllAdminProducts(Integer offset, Integer limit, Model model){

        PageInfo<TProduct> pageInfo = tProductService.queryAllByPageHelper(offset, limit);
        System.out.println(pageInfo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("msg", "反对大师傅");
//        return pageInfo;
        return "/admin/all_products";
    }

    @RequestMapping("index")
    public String index(Integer id, Model model, HttpSession session){
        TProduct tProduct = tProductService.queryById(id);
        Integer num = tProduct.getLookNumber() + 1;
        tProduct.setLookNumber(num);
        tProductService.update(tProduct);
        model.addAttribute("product", tProduct);


        login = (TUser) session.getAttribute("user");
        List<TCart> list = tCartService.findAllCartAssociateProductByUserId(login.getId());//session.getAttribute("user").getId
        TCartController.cartBar(list, model);
        return "user/single-product-simple";
    }

    @RequestMapping("searchProduct")
    public String searchProduct(Integer offset, Integer limit, String search, Model model, HttpSession session){
        PageInfo<TProduct> pageInfo = tProductService.vagueFindAllProducts(offset, limit, search);
        model.addAttribute("pageInfo",pageInfo);

        login = (TUser) session.getAttribute("user");
        List<TCart> list = tCartService.findAllCartAssociateProductByUserId(login.getId());//session.getAttribute("user").getId
        TCartController.cartBar(list, model);
        return "user/shop";
    }

    @RequestMapping("addCart")
    @ResponseBody
    public String addCart(Integer productId, Integer count, Model model, HttpSession session){

        login = (TUser) session.getAttribute("user");
//        List<TCart> list = tCartService.findAllCartAssociateProductByUserId(login.getId());
        TCart tCart = new TCart();
        tCart.setUserId(login.getId());//session.getAttribute("user").getId();
        tCart.setProductId(productId);
        tCart.setCount(count);
        String msg = tCartService.userAddCart(tCart);
//        TProduct tProduct = tProductService.queryById(productId);
//        model.addAttribute("product", tProduct);

//        List<TCart> list = tCartService.findAllCartAssociateProductByUserId(login.getId());//session.getAttribute("user").getId
//        TCartController.cartBar(list, model);
        return msg;
//        return "user/single-product-simple";
    }


    @RequestMapping("/admin/searchProduct")
//    @ResponseBody
    public String adsearchProduct(Integer offset, Integer limit, String search, Model model , HttpSession session){

        PageInfo<TProduct> pageInfo = tProductService.vagueFindAllProducts(offset, limit, search);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("search", search);
        return "admin/all_products";
//        return pageInfo;
    }
}